<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '50px' }"
          disabled
        >
          <div class="title-bar" @click="goIndex" key="/">
            <img class="logo" src="../assets/oj-logo.svg" />
            <div class="title">{{ COMMON_ENUM.PROJECT_NAME }}</div>
          </div>
        </a-menu-item>
        <!-- todo 一级菜单单独写出来，二级菜单用v-for写-->
        <a-menu-item key="/">首页</a-menu-item>
        <!-- v-if和v-for不能公用-->
        <a-sub-menu v-for="item in visibleRoutes" :key="item.path">
          <template #title>{{ item.name }}</template>
          <a-menu-item v-for="ele in filterSonMenu(item)" :key="ele.path">
            {{ ele.name }}
          </a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-col>
    <a-col flex="200px" :style="{ padding: 0, marginRight: '50px' }">
      <!-- todo 添加登录注册按钮动作方法-->
      <a-button-group>
        <a-button
          type="text"
          status="success"
          v-if="
            store.state.user?.loginUser?.userName ==
            COMMON_ENUM.NOT_LOGIN_USERNAME
          "
          >登录
        </a-button>
        <a-button
          type="text"
          status="warning"
          v-if="
            store.state.user?.loginUser?.userName ==
            COMMON_ENUM.NOT_LOGIN_USERNAME
          "
          >注册
        </a-button>
        <div :style="{ marginLeft: '30px' }">
          {{
            store.state.user?.loginUser?.userName ??
            COMMON_ENUM.NOT_LOGIN_USERNAME
          }}
        </div>
      </a-button-group>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { computed, ref } from "vue";
import { routes } from "@/router/routes";
import checkAccess from "@/access/checkAccess";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import COMMON_ENUM from "../common/enum/commonEnum";

const router = useRouter();
const store = useStore();

// 展示在菜单的路由数组
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // todo 根据权限过滤菜单
    // if (
    //   !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    // )
    // {
    //   return false;
    // }
    return true;
  });
});

// 过滤二级子菜单
const filterSonMenu = (item: any) => {
  if (item.children) {
    // 过滤掉那些 meta.hideMenu 为 true 的子菜单项
    item.children = item.children.filter(
      (child: any) => !child.meta?.hideInMenu
    );
    // todo 权限控制
  }
  return item.children;
};

// 默认主页
const selectedKeys = ref(["/"]);

// 路由跳转后，更新选中的菜单项
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

// 点击logo回到首页
const goIndex = () => {
  router.push("/");
};

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title-bar:hover {
  cursor: pointer;
}

.title {
  color: #444;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
